import { query as queryUser, fakequeryCurrent } from '../services/user';
import { resetpwd, queryCurrent, saveProfile } from '../services/api';
import { message } from 'antd';
export default {
  namespace: 'user',

  state: {
    list: [],
    currentUser: {},
  },

  effects: {
    *fetch(_, { call, put }) {
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchCurrent(_, { call, put }) {
      const response = yield call(queryCurrent);
      yield put({
        type: 'saveCurrentUser',
        payload: response.amUserInfo,
      });
    },
    *resetPwd({ payload, callback }, { call }) {
      const response = yield call(resetpwd, payload);
      if (response?.result?.code !== 1000) {
        message.error('提交出错，请检查');
      } else if (callback) callback();
    },
    *saveprofile({ payload, callback }, { call, put }) {
      const response = yield call(saveProfile, payload);
      if (response?.amSaveUserInfo === 'undefined') {
        message.error('提交出错，请检查');
      } else {
        if (callback) {
          callback();
        }
        yield put({
          type: 'saveCurrentUser',
          payload: response.amSaveUserInfo,
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveCurrentUser(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          ...action.payload,
        },
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload,
        },
      };
    },
  },
};
